package com.example.demo.controller;

import com.example.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author :蕉绿
 * @Date : 2023/2/17 19:38
 * @Version : V1.0
 * @Description :
 */

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @RequestMapping(value = "/hi")
    public String sayHi() {
        return "hi,Spring MVC!";
    }
    @PostMapping("/post")
    public void sayPost() {
        System.out.println("hi post");
    }
    @GetMapping("/get")
    public String sayGet(String name,int v) {
        return "hi,Spring MVC! get" + name + "  v |" +v ;
    }

    //获取普通对象
    @GetMapping("/showuser")
    public String shower(User user) {
        return user.toString();
    }

    //后端参数重命名 @RequestParam
    @GetMapping("/showParam")
    public String showParam(@RequestParam(value = "t",required = false) String starTime,@RequestParam(value = "t2",required = false) String endTime) {
        return "开始时间: " + starTime + "  结束时间: " + endTime;
    }

    //获取json 对象
        @GetMapping("/showJson")
    public String showJson(@RequestBody User user) {

        return user.toString();
    }

    //简洁  SEO优化
    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username")String username,@PathVariable("password")String password) {

        return username + ": " + password;
    }

    //文件上传
    @RequestMapping("/upfile")
    public String uoFile(@RequestPart("myfile")MultipartFile file) throws IOException {

        String path = "D:\\logs\\img.png" ;
        file.transferTo(new File(path));
        return path;
    }

    @Value("${mypath}")
    private String mypath;

    //文件上传最终版
    @RequestMapping("/myupfile")
    public String myUpFile(@RequestPart("myfile")MultipartFile file) throws IOException {

        String path = mypath;
        path += UUID.randomUUID().toString().replace("-","");
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));
        return path;
    }

    @GetMapping("/getparam")
    public String getParam(HttpServletRequest request) {
        return request.getParameter("username");
    }

    @RequestMapping("/getck")
    public String getCK(HttpServletRequest req) {
        //获取全部cookies
        Cookie[] cookies = req.getCookies();
        for(Cookie item:cookies) {
            log.error(item.getName() + ":" + item.getValue());
        }

        return "Hi! Cookies!";
    }

    //获取单个cookie
    @RequestMapping("/getck2")
    public String getCk2(@CookieValue("zhangsan")String val) {
        return val;
    }

    //获取Header
    @RequestMapping("/getheader")
    public String getHeader(@RequestHeader("User-Agent")String userAgent) {

        return userAgent;
    }
    @RequestMapping("/setsession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.setAttribute("userinfo","userinfo");
        return "Set session Success!";
    }
    @RequestMapping("/getsession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute("userinfo") != null) {
                return (String)session.getAttribute("userinfo");
        } else {
            return "无session";
        }
    }

    //更简单的获取session
    @RequestMapping("/getsession2")
    public String getSession2(@SessionAttribute(value = "userinfo",required = false)String userinfo) {

            return  userinfo;

    }



}
